package com.itsci.assignment2;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ShowGuide extends Activity {
	
	private TextView name;
	private TextView tel;
	private TextView email;
	private GuideManager guideMgr;
	private GuideBean guideBean;
	private Button btnTel;
	private Button btnMgs;
	private Button btnBack;
	private Button btnEdit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_guide);
		name = (TextView)findViewById(R.id.textView_Show_guide_name);
		tel = (TextView)findViewById(R.id.textView_Show_guide_tel);
		email = (TextView)findViewById(R.id.textView_Show_guide_email);
		Intent intent = getIntent();
		String intentName = intent.getStringExtra("name");
		guideMgr = new GuideManager(ShowGuide.this, null, null, 1);
		guideBean = guideMgr.getByGuideId(intentName);
		name.setText(guideBean.getGuidName());
		tel.setText(guideBean.getTel());
		email.setText(guideBean.getEmail());
		btnTel = (Button)findViewById(R.id.button_Show_guide_tel);
		btnMgs = (Button)findViewById(R.id.button_Show_guide_mgs);
		
		btnTel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String uri = "tel:"+tel.getText().toString();
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse(uri));
				startActivity(intent);				
			}
		});
		btnMgs.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent sendIntent = new Intent(Intent.ACTION_SENDTO,
						Uri.parse("sms:" + tel.getText().toString()));
				startActivity(sendIntent);
				
			}
		});
		
		btnBack = (Button)findViewById(R.id.button_show_guide_back);
		btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent back = new Intent(ShowGuide.this,ListGuide.class);
				startActivity(back);
			}
		});
		
		btnEdit = (Button)findViewById(R.id.button_Show_guide_editGuide);
		btnEdit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent gotoEdit = new Intent(ShowGuide.this,EditGuide.class);
				gotoEdit.putExtra("name",name.getText().toString());
				gotoEdit.putExtra("tel",tel.getText().toString());
				gotoEdit.putExtra("email",email.getText().toString());
//				startActivity(gotoEdit);
				startActivityForResult(gotoEdit, 111);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Toast.makeText(getBaseContext(), Integer.toString(resultCode), Toast.LENGTH_SHORT).show();		
		if(resultCode ==111){		
			name.setText(data.getExtras().getString("name"));
			tel.setText(data.getStringExtra("tel"));
			email.setText(data.getStringExtra("email"));		
		}
	}
}
